package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AppProcurementDetailBo;
import org.dromara.system.domain.vo.AppProcurementDetailVo;
import org.dromara.system.domain.AppProcurementDetail;
import org.dromara.system.mapper.AppProcurementDetailMapper;
import org.dromara.system.service.IAppProcurementDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 采购详情Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppProcurementDetailServiceImpl implements IAppProcurementDetailService {

    private final AppProcurementDetailMapper baseMapper;

    /**
     * 查询采购详情
     *
     * @param id 主键
     * @return 采购详情
     */
    @Override
    public AppProcurementDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询采购详情列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 采购详情分页列表
     */
    @Override
    public TableDataInfo<AppProcurementDetailVo> queryPageList(AppProcurementDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppProcurementDetail> lqw = buildQueryWrapper(bo);
        Page<AppProcurementDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的采购详情列表
     *
     * @param bo 查询条件
     * @return 采购详情列表
     */
    @Override
    public List<AppProcurementDetailVo> queryList(AppProcurementDetailBo bo) {
        LambdaQueryWrapper<AppProcurementDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppProcurementDetail> buildQueryWrapper(AppProcurementDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppProcurementDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppProcurementDetail::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getPlanCode()), AppProcurementDetail::getPlanCode, bo.getPlanCode());
        lqw.eq(bo.getMaterialTypeId() != null, AppProcurementDetail::getMaterialTypeId, bo.getMaterialTypeId());
        lqw.eq(bo.getCategoryId() != null, AppProcurementDetail::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getPlannedWeight() != null, AppProcurementDetail::getPlannedWeight, bo.getPlannedWeight());
        lqw.eq(bo.getPlannedTruckNum() != null, AppProcurementDetail::getPlannedTruckNum, bo.getPlannedTruckNum());
        return lqw;
    }

    /**
     * 新增采购详情
     *
     * @param bo 采购详情
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppProcurementDetailBo bo) {
        AppProcurementDetail add = MapstructUtils.convert(bo, AppProcurementDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改采购详情
     *
     * @param bo 采购详情
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppProcurementDetailBo bo) {
        AppProcurementDetail update = MapstructUtils.convert(bo, AppProcurementDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppProcurementDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除采购详情信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void insertBatch(List<AppProcurementDetail> appProcurementDetails) {
        baseMapper.insertBatch(appProcurementDetails);
    }

    @Override
    public List<AppProcurementDetail> queryListByPlanCode(String planCode) {
        LambdaQueryWrapper<AppProcurementDetail> appProcurementDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appProcurementDetailLambdaQueryWrapper.eq(AppProcurementDetail::getPlanCode, planCode);
        return baseMapper.selectList(appProcurementDetailLambdaQueryWrapper);
    }

}
